package com.evil.mi.controller;

import com.evil.mi.bean.VideoBean;
import com.evil.mi.entity.PageEntity;
import com.evil.mi.entity.VideoEntity;
import com.evil.mi.enums.VideoStateEnum;
import com.evil.mi.service.CrawlerService;
import com.evil.mi.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * com.evil.mi.controller
 *
 * @author 白雨浓
 * date: 2018/8/18 16:14
 * content:
 */
@Controller
public class VideoController {

    @Value("${http}")
    private String HTTP;

    @Autowired
    private VideoService videoService;

    @Autowired
    private CrawlerService crawlerService;


    @RequestMapping("/")
    public String index() {
        return "redirect:/list/1";
    }

    @RequestMapping("/list/{current}")
    public String indexPage(@PathVariable int current, Model model) {

        PageEntity<VideoEntity> pageEntity = videoService.findVideoPage(current, 13);

        model.addAttribute("current", current);
        model.addAttribute("size", pageEntity.getSize());
        model.addAttribute("total", pageEntity.getTotal());
        model.addAttribute("list", convert(pageEntity));
        model.addAttribute("start", crawlerService.getStatus());
        model.addAttribute("http", HTTP);

        return "list";
    }

    @RequestMapping("/alternative/{current}")
    public String alternativePage(@PathVariable int current, Model model) {

        PageEntity<VideoEntity> pageEntity = videoService.findVideoAlternativePage(current, 20);

        model.addAttribute("current", current);
        model.addAttribute("size", pageEntity.getSize());
        model.addAttribute("total", pageEntity.getTotal());
        model.addAttribute("list", convert(pageEntity));
        model.addAttribute("start", crawlerService.getStatus());
        model.addAttribute("http", HTTP);

        return "alternative";
    }

    @RequestMapping("/video/like/{videoId}")
    public String likeVideo(@PathVariable int videoId, String from, int current) {

        videoService.modifyVideoState(videoId, VideoStateEnum.ALTERNATIVE);

        return "redirect:/" + from + "/" + current;
    }

    @RequestMapping("/video/dislike/{videoId}")
    public String dislikeVideo(@PathVariable int videoId, String from, int current) {

        videoService.modifyVideoState(videoId, VideoStateEnum.INIT);

        return "redirect:/" + from + "/" + current;
    }


    @RequestMapping("/video/delete/{videoId}")
    public String deleteVideo(@PathVariable int videoId, String from, int current) {

        videoService.removeVideo(videoId);

        return "redirect:/" + from + "/" + current;
    }


    private List<VideoBean> convert(PageEntity<VideoEntity> pageEntity) {
        return pageEntity.getData().stream().map(entity -> {
            VideoBean bean = new VideoBean();
            BeanUtils.copyProperties(entity, bean);
            bean.setState(entity.getState().getCode());
            bean.setStateMsg(entity.getState().getMsg());
            return bean;
        }).collect(Collectors.toList());
    }

}
